package com.esm.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.esm.model.User;
import com.esm.service.FindSelfInfoService;
import com.esm.service.GetMyCallMsgReService;
import com.esm.serviceimpl.FindSelfInfoServiceImpl;
import com.esm.serviceimpl.GetMyCallMsgReServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class GetMyCallMsgReController extends HttpServlet{
	GetMyCallMsgReService ms=new GetMyCallMsgReServiceImpl();
	FindSelfInfoService fs=new FindSelfInfoServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf8");
		HttpSession session=req.getSession();
		User curUser=(User) session.getAttribute("currentUser");
		int curuid=curUser.getUid();
		String lgroup=req.getParameter("lgroup");
		JSONArray callMsgRe=ms.getMyCallMsgReturn(lgroup);
		for(int i=0;i<callMsgRe.size();i++){
			JSONObject curCallMsgRe=callMsgRe.getJSONObject(i);
			int uid=curCallMsgRe.getInt("uid");
			JSONObject u=fs.findUserInfo(uid);
			String uname=u.getString("uname");
			curCallMsgRe.put("uname", uname);
		}
		JSONObject j=new JSONObject();
		j.put("curuid", curuid);
		j.put("callMsgRe", callMsgRe);
		PrintWriter pw=resp.getWriter();
		pw.write(j.toString());
		pw.flush();
		pw.close();
	}
}
